package cn.com.xinjinfu.pdoulcProduct.activity.recharge;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.EditTextUtil;
import cn.com.xinjinfu.pdoulcProduct.view.PwdEditText;

/**
 * Created by android-adt on 2016/8/3.
 */
public class PayPwdDilog {
    private RequestQueue requen;
    private Context mContext;
    private String mMoney;
    private SuccessListener mSuccessListener;
    private TextView tv_error_hint;
    public Dialog dialog;
    public interface SuccessListener {
        /**
         * 密码验证成功监听
         */
        void onPwdSuccess(String password);
    }
    /**
     * 设置输入完成监听
     */
    public void setSuccessListener(SuccessListener successListener) {
        this.mSuccessListener = successListener;
    }
    /***
     *  支付密码
     */
    public PayPwdDilog(Context context,String money){
        mContext = context;
        mMoney = money;
        showPayPwdDilog();
    }

    public void showPayPwdDilog() {
        dialog = new Dialog(mContext, R.style.dialog_tran);
        dialog.setContentView(R.layout.dialog_pay_pwd);
        LinearLayout ll_transfer = (LinearLayout) dialog.findViewById(R.id.ll_transfer);
        TextView tv_transfer_money = (TextView) dialog.findViewById(R.id.tv_transfer_money);

        tv_error_hint = (TextView) dialog.findViewById(R.id.tv_error_hint);
        TextView tv_forget_pwd = (TextView) dialog.findViewById(R.id.tv_forget_pwd);
        PwdEditText pet_pwd = (PwdEditText) dialog.findViewById(R.id.pet_pwd);
        EditTextUtil.setUpSofe(pet_pwd);
        pet_pwd.setOnInputFinishListener(new PwdEditText.OnInputFinishListener() {
            @Override
            public void onInputFinish(String password) {
                requen = Volley.newRequestQueue(mContext);
                pwdcheckRequen(password);
            }

            @Override
            public void onInputChanged() {
                tv_error_hint.setText("");
            }
        });
        if (mMoney == null){
            ll_transfer.setVisibility(View.GONE);
        }else {
            ll_transfer.setVisibility(View.VISIBLE);
            tv_transfer_money.setText(mMoney);
        }
        tv_forget_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, ForgetPayPwdActivity.class));
            }
        });
        dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public void pwdcheckRequen(final String password) {
        final Map<String, String> parameters = DataHandler.getParameters("1403");
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            parameters.put("id", user.getCurrUser().getSign());
        }
        parameters.put("passwd", DataHandler.encrypt3DES(password));
        parameters.put("type", "2");
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jobj) {
                L.i("校验密码返回结果" + jobj.toString());
                if (JSONManager.getError(jobj) == -1) {
                    mSuccessListener.onPwdSuccess(password);
                    dialog.dismiss();
                }else {
                    tv_error_hint.setText(JSONManager.getMsg(jobj));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                DataHandler.errorHandler(mContext, error);
            }
        });
        requen.add(request);
    }
}
